﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Queries;

namespace Mang24k99.Model.COMM
{  
    [ActiveRecord("KT_COMM_CODETYPE")]
    public class MKtCommCodeType : ActiveRecordBase
    {
        #region Model

        private int _id;
        private int? _type_code;
        private string _type_name;
        private int? _type_spec;
        private string _type_remark;
        private int? _is_validate;
        private DateTime? _input_time;
        private DateTime? _modify_time;
        private DateTime? _delete_time;

        /// <summary>
        /// 自增ID
        /// </summary>
        [Property("ID", Update = false, Insert = false)]
        public int ID
        {
            set { _id = value; }
            get { return _id; }
        }

        /// <summary>
        /// 主键类型编码
        /// </summary>
        [PrimaryKey(PrimaryKeyType.Assigned,"TYPE_CODE")]
        public int? TYPE_CODE
        {
            set { _type_code = value; }
            get { return _type_code; }
        }


        /// <summary>
        ///类型名称 
        /// </summary>
        [Property("TYPE_NAME")]
        public string TYPE_NAME
        {
            set { _type_name = value; }
            get { return _type_name; }
        }

        /// <summary>
        /// 编码分类
        /// </summary>
        [Property("TYPE_SPEC")]
        public int? TYPE_SPEC
        {
            set { _type_spec = value; }
            get { return _type_spec; }
        }
        
        /// <summary>
        /// 备注
        /// </summary>
        [Property("TYPE_REMARK")]
        public string TYPE_REMARK
        {
            set { _type_remark = value; }
            get { return _type_remark; }
        }

        /// <summary>
        /// 是否有效
        /// </summary>
        [Property("IS_VALIDATE")]
        public int? IS_VALIDATE
        {
            set { _is_validate = value; }
            get { return _is_validate; }
        }

        /// <summary>
        /// 输入时间
        /// </summary>
        [Property("INPUT_TIME")]
        public DateTime? INPUT_TIME
        {
            set { _input_time = value; }
            get { return _input_time; }
        }

        /// <summary>
        /// 修改时间
        /// </summary>
        [Property("MODIFY_TIME")]
        public DateTime? MODIFY_TIME
        {
            set { _modify_time = value; }
            get { return _modify_time; }
        }

        /// <summary>
        /// 删除时间
        /// </summary>
        [Property("DELETE_TIME")]
        public DateTime? DELETE_TIME
        {
            set { _delete_time = value; }
            get { return _delete_time; }
        }

        private IList<MKtCommCodeList> _commcode;

        [HasMany(typeof(MKtCommCodeList), Table = "KT_COMM_CODELIST", ColumnKey = "TYPE_CODE")]
        public IList<MKtCommCodeList> MKtCommCodeList
        {
            get { return _commcode; }
            set { _commcode = value; }
        }

        #endregion Model

        #region Static Methods

        protected override void OnDelete()
        {
            MKtCommCodetypeHistory mHistory = new MKtCommCodetypeHistory();
            Common.ModelCommon.CopyObjectValues(this, mHistory);
            mHistory.DELETE_TIME = DateTime.Now;
            mHistory.Create();
            base.OnDelete();
        }
        /// <summary>
        /// 创建一条记录
        /// </summary>
        /// <param name="commbase"></param>
        public static void Create(MKtCommCodeType codetype)
        {
            codetype.Create();
        }

        /// <summary>
        /// 选择所有记录
        /// </summary>
        public static MKtCommCodeType[] SelectAll()
        {
            return (MKtCommCodeType[])FindAll(typeof(MKtCommCodeType));
        }
        /// <summary>
        /// 修改一条记录
        /// </summary>
        /// <param name="codetype"></param>
        public static void Update(MKtCommCodeType codetype)
        {
            codetype.Update();
        }
        //根据主键选择一条记录
        public static MKtCommCodeType Select(int typecode) 
        {
            return (MKtCommCodeType)FindByPrimaryKey(typeof(MKtCommCodeType),typecode);
        }
        /// <summary>
        /// 物理删除代码类型，同时删除该类型下所有数据
        /// </summary>
        /// <param name="codetype"></param>
        public static void Delete(MKtCommCodeType codetype)
        {
            MKtCommCodeList[] codelist;
            codelist = (MKtCommCodeList[])FindAllByProperty(typeof(MKtCommCodeList), "MKtCommCodeType.TYPE_CODE", codetype.TYPE_CODE);
            for (int i = 0; i < codelist.Length; i++)
            {
                codelist[i].Delete();
            }
            codetype.Delete();
        }
        /// <summary>
        /// 根据某Property列返回符合条件的记录条数
        /// </summary>
        /// <returns></returns>
        public static int SelectByProperty(string strPro, object Value)
        {
            MKtCommCodeType[] baseinfo;
            baseinfo = (MKtCommCodeType[])FindAllByProperty(typeof(MKtCommCodeType), strPro, Value);
            return baseinfo.Length;
        }
       /// <summary>
       /// 返回第start条记录后的max条记录
       /// </summary>
       /// <param name="start"></param>
       /// <param name="max"></param>
       /// <param name="where"></param>
       /// <returns></returns>
        public static IList<MKtCommCodeType> FindALLSliced(int start, int max, string where)
        {
            string hql = @"from MKtCommCodeType codetype " + where;
            SimpleQuery query = new SimpleQuery(typeof(MKtCommCodeType), hql);
            query.SetQueryRange(start, max);
            return (IList<MKtCommCodeType>)ExecuteQuery(query);
        }
        /// <summary>
        /// 根据where条件返回符合条件记录数
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public static int GetCount(string where)
        {
            string hql = @"select count(*) from MKtCommCodeType codetype " + where;
            ScalarQuery query = new ScalarQuery(typeof(MKtCommCodeType), hql);
            return Convert.ToInt32(ExecuteQuery(query));
        }
        #endregion Static Methods
    }
}
